knitr::opts_chunk$set(fig.path = "README_figs/README-")
library(tidyverse)
library(plotly)
library(highcharter)
library(leaflet)
library(rmarkdown)
library(flexdashboard)
library(shiny)
library(forecast)
library(DT)
## Rows: 108
## Columns: 4
## Groups: is_china [2]
## $ is_china  <chr> "China", "China", "China", "China", "China", "China"...
## $ date      <date> 2020-01-22, 2020-01-23, 2020-01-24, 2020-01-25, 202...
## $ cases     <dbl> 548, 95, 277, 486, 669, 802, 2632, 578, 2054, 1661, ...
## $ cum_cases <dbl> 548, 643, 920, 1406, 2075, 2877, 5509, 6087, 8141, 9...

Data Visualization https://www.udemy.com/course/r-tidyverse-reporting-and-analytics-for-excel-users/learn/lecture/8820024#overview

cars %>% plot()

forecast(AirPassengers) %>% plot()

ggplot(mpg) +
  geom_point(mapping = aes(x=displ, y = hwy, color = class, size = year, alpha=model))

ggplot(confirmed_cases_china_vs_world) +
  geom_line(aes(x = date, y = cum_cases, group = is_china, color = is_china)) +
  ylab("Cumulative Confirmed Cases")

ggplot(confirmed_cases_china_vs_world, aes(x = date, y = cum_cases, group = is_china, color = is_china)) +
  geom_line() +
  geom_point() +
  ylab("Cumulative Confirmed Cases")

Different Bar Charts

ggplot(mpg) + 
  geom_bar(mapping = aes(x=displ))

ggplot(mpg) + 
  geom_col(mapping = aes(x=displ, y=hwy, fill=class))

GGPlotly

g <- ggplot(mpg) + 
  geom_point(aes(x=displ,y=hwy,color=class))

ggplotly(g)
hchart(mpg, "scatter",
       hcaes(x=displ, y = hwy, group = class))
forecast(AirPassengers) %>% hchart()

Boxplot

ggplot(mpg) + 
  geom_boxplot(mapping = aes(x=class, y=hwy))

Allows you to space out the dots rather than them being on top of eachother

ggplot(mpg) + 
  geom_jitter(mapping = aes(x=displ, y = hwy))

Labelying X or y axis as percent

ggplot(mpg) + 
  geom_jitter(mapping = aes(x=displ, y = hwy/100)) +
  scale_y_continuous(labels = scales::percent)

Aids the eye in seeing patterns in the presence of overplotting. geom_smooth() and stat_smooth() are effectively aliases: they both use the same arguments. Use stat_smooth() if you want to display the results with a non-standard geom.

ggplot(mpg) + 
  geom_smooth(mapping = aes(x=displ, y = hwy))

ggplot(mpg) + 
  geom_smooth(mapping = aes(x=displ, y = hwy),method = 'lm')

ggplot(mpg,mapping = aes(x=displ,y=hwy)) +
  geom_jitter(mapping=aes(color=class)) +
  geom_smooth() +
  annotate("rect",xmin=1.5,xmax=2,ymin=40,ymax = 45, color="red", alpha = 0.3) +
  annotate("text",x=3,y=43,label="Let's highlight these \n outliers") +
  ggtitle("Lets lavel our chart","Add your subtitle here")

who_events <- tribble(
  ~ date, ~ event,
  "2020-01-30", "Global health\nemergency declared",
  "2020-03-11", "Pandemic\ndeclared",
  "2020-02-13", "China reporting\nchange"
) %>%
  mutate(date = as.Date(date))

plt_cum_confirmed_cases_china_vs_world <- ggplot(confirmed_cases_china_vs_world) +
  geom_line(aes(x = date, y = cum_cases, group = is_china, color = is_china)) +
  ylab("Cumulative Confirmed Cases")

plt_cum_confirmed_cases_china_vs_world +
  geom_vline(aes(xintercept = date), data = who_events, linetype = "dashed") +
  geom_text(aes(x = date ,label = event), data = who_events, y = 35000)

# Filter for China, from Feb 15
china_after_feb15 <- confirmed_cases_china_vs_world %>%
  filter(is_china == "China", date >= "2020-02-15")

# Using china_after_feb15, draw a line plot cum_cases vs. date
# Add a smooth trend line using linear regression, no error bars
ggplot(china_after_feb15, aes(x = date, y = cum_cases)) +
  geom_line() +
  geom_smooth(method = "lm", se = FALSE) +
  ylab("Cumulative confirmed cases")

# Filter confirmed_cases_china_vs_world for not China
not_china <- confirmed_cases_china_vs_world  %>% 
    filter(is_china != 'China')

# Using not_china, draw a line plot cum_cases vs. date
# Add a smooth trend line using linear regression, no error bars
plt_not_china_trend_lin <- ggplot(not_china, aes(x = date, y = cum_cases)) +
  geom_line() +
  geom_smooth(method = "lm", se = FALSE) +
  ylab("Cumulative confirmed cases")

# See the result
plt_not_china_trend_lin 

### From the plot above, we can see a straight line does not fit well at all, and the rest of the world is growing much faster 
### than linearly. What if we added a logarithmic scale to the y-axis?

plt_not_china_trend_lin + 
  scale_y_log10()

ggplot(mpg) + 
  geom_jitter(aes(x=displ, y = hwy,color=class)) +
  facet_wrap(~class)

ggplot(mpg) + 
  geom_jitter(aes(x=displ, y = hwy,color=class)) +
  facet_wrap(year~class)

#***************************************************************************
### Interactive Data Tables
### https://www.udemy.com/course/r-tidyverse-reporting-and-analytics-for-excel-users/learn/lecture/8797450#overview

library(DT)

mpg %>% datatable()
mpg %>% datatable(rownames = F)
mpg %>% datatable(rownames = F) %>% 
  formatCurrency("displ",currency = "$", digits = 2)
mpg %>% datatable(rownames = F) %>% 
  formatCurrency("displ",currency = "$", digits = 2)
ctyPercentile <- mpg$cty %>% quantile(c(.25,.75))

mpg %>% datatable(rownames = F) %>% 
  formatCurrency("displ",currency = "$", digits = 2) %>% 
  formatStyle("cty",
      backgroundColor = styleInterval(ctyPercentile,c("green","yellow","red")),
      color = styleInterval(ctyPercentile,c("white","blue","white"))
              ) %>% 
  formatStyle("hwy",background = styleColorBar(mpg$hwy,"steelblue"))

Different Ways to set figure size in RMarkdown https://sebastiansauer.github.io/figure_sizing_knitr/

### {r fig1, fig.height = 3, fig.width = 5}

ggplot(mpg) + 
  geom_jitter(mapping = aes(x=displ, y = hwy/100)) +
  scale_y_continuous(labels = scales::percent)